<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use services\models\minisite\MActivityShowFeel;

/**
 * MActivityShowFeelSearch represents the model behind the search form about `common\models\MActivityShowFeel`.
 */
class MActivityShowFeelSearch extends MActivityShowFeel
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'as_id', 'c_category', 'c_status', 'uid', 'op_id', 'c_score', 'c_channel'], 'integer'],
            [['c_name', 'c_content', 'c_image', 'db_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = MActivityShowFeel::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 20,
            ],
            'sort'=>[
                'defaultOrder'=>[
                    'db_time'=>SORT_DESC
                ]
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $params = [
            'id' => $this->id,
            'as_id' => $this->as_id,
            'c_category' => $this->c_category,
            'c_status' => (int)$this->c_status,
            'c_score' => $this->c_score,
            'uid' => $this->uid,
            'db_time' => $this->db_time,
            'op_id' => $this->op_id,
            'c_channel' => $this->c_channel,
        ];
        if($this->c_category == -1 ){
            unset($params['c_category']);
        }
        if($this->c_channel == -1 ){
            unset($params['c_channel']);
        }

        // grid filtering conditions
        $query->andFilterWhere($params);

        $query->andFilterWhere(['like', 'c_name', $this->c_name])
            ->andFilterWhere(['like', 'c_content', $this->c_content])
            ->andFilterWhere(['like', 'c_image', $this->c_image]);

        return $dataProvider;
    }

    public function getStatusStr()
    {
        return self::$STATUS[$this->c_status];

    }

    public function addShowTimes()
    {
        $this->updateAllCounters(['c_view_times'=>1]);
    }

    public function getStatistics()
    {
        return $this->hasOne(MActivityShowFeelStatistics::className(), ['asf_id'=>'id']);
    }


}
